package com.grass.cropTradingCenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grass.cropTradingCenter.mapper.UserCommitMapper;
import com.grass.cropTradingCenter.pojo.entity.Goods;
import com.grass.cropTradingCenter.pojo.entity.MiniShop;
import com.grass.cropTradingCenter.pojo.entity.UserCommit;
import com.grass.cropTradingCenter.pojo.entity.UserOrder;
import com.grass.cropTradingCenter.pojo.po.QueryImage;
import com.grass.cropTradingCenter.service.*;
import com.grass.cropTradingCenter.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author zipan
* @description 针对表【user_commit】的数据库操作Service实现
* @createDate 2024-04-05 22:17:07
*/
@Service
public class UserCommitServiceImpl extends ServiceImpl<UserCommitMapper, UserCommit>
    implements UserCommitService{

    @Autowired
    UserOrderService userOrderService;

    @Autowired
    ImageService imageService;

    @Autowired
    GoodsService goodsService;


    @Autowired
    MiniShopService miniShopService;

    @Autowired
    MiniUserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCommit(UserCommit commit) {
        UserOrder order = userOrderService.getById(commit.getOrderId());
        commit.setGoodsId(order.getGoodsId());
        commit.setUserId(SecurityUtils.getUserId());
        commit.setShopId(order.getShopId());
        this.save(commit);
        if (commit.getImageList() != null && commit.getImageList().size() >= 1) {
            imageService.saveImages(new QueryImage(commit.getId(), 2), commit.getImageList());
        }
        Goods goods = goodsService.getById(order.getGoodsId());
        goods.setCommitNum(goods.getCommitNum() + 1);
        goodsService.updateById(goods);

        order.setOrderType(5);
        userOrderService.updateById(order);

        MiniShop shop = miniShopService.getById(order.getShopId());
        // 差评
        if(commit.getCommitRate() == 1){
            shop.setBadEvaluateNum(shop.getBadEvaluateNum() + 1);
        }else if (commit.getCommitRate() == 2){
            shop.setGeneralEvaluateNum(shop.getGeneralEvaluateNum() + 1);
        }else if(commit.getCommitRate() == 3){
            shop.setGoodEvaluateNum(shop.getGoodEvaluateNum() + 1);
        }
        miniShopService.updateById(shop);
    }

    @Override
    public List<UserCommit> commitList() {
        LambdaQueryWrapper<UserCommit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserCommit::getUserId, SecurityUtils.getUserId());
        queryWrapper.orderByDesc(UserCommit::getId);
        List<UserCommit> list = this.list(queryWrapper);

        for (UserCommit commit : list) {
            commit.setImageList(imageService.getImageList(new QueryImage(commit.getId(), 2)));
            commit.setOrder(userOrderService.getById(commit.getOrderId()));
        }

        return list;
    }

    @Override
    public List<UserCommit> commitList(Integer goodsId) {
        LambdaQueryWrapper<UserCommit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserCommit::getGoodsId, goodsId);
        queryWrapper.orderByDesc(UserCommit::getId);
        List<UserCommit> list = this.list(queryWrapper);

        int userId = SecurityUtils.getUserId();
        List<UserCommit> collect = list.stream().filter(commit -> {
            return commit.getCommitType() == 1 || commit.getUserId() == userId;
        }).collect(Collectors.toList());

        for (UserCommit commit : collect) {
            commit.setImageList(imageService.getImageList(new QueryImage(commit.getId(), 2)));
            commit.setOrder(userOrderService.getById(commit.getOrderId()));
            commit.setUser(userService.getById(commit.getUserId()));
        }
        return collect;
    }

    @Override
    public Integer getGoodEvaluateNum(int goodsId) {
        List<UserCommit> list = commitList(goodsId);
        return (int) list.stream().filter(e -> e.getCommitRate() == 3).count();
    }
}




